package com.tjsinfo.tjsoa.fileExpress.bo;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.common.DayFormat;
import com.tjsinfo.tjsoa.fileExpress.dao.IFileExpressFileDAO;
import com.tjsinfo.tjsoa.fileExpress.vo.FileExpressFile;

public class FileExpressFileBo implements IFileExpressFileBo {
	private IFileExpressFileDAO expressFileDAO;
	private IJDBCDao jdbcdao;
	private IQbcDao qbcDao;

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public void setJdbcdao(IJDBCDao jdbcdao) {
		this.jdbcdao = jdbcdao;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFileBo#setExpressFileDAO(com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFileDAO)
	 */
	public void setExpressFileDAO(IFileExpressFileDAO expressFileDAO) {
		this.expressFileDAO = expressFileDAO;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFileBo#save(com.tjsinfo.tjsoa.fileExpress.FileExpress_File.FileExpressFile)
	 */
	public void save(FileExpressFile file) {
		expressFileDAO.save(file);
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFileBo#update(com.tjsinfo.tjsoa.fileExpress.FileExpress_File.FileExpressFile)
	 */
	public void update(FileExpressFile file) {
		expressFileDAO.update(file);
	}
	
	public void updatejdbc(String id,int state) {
		String sql = "update t_fileExpress_file set state="+state+" where id='"+id+"'";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
	}
	
	public void updatejdbcName(String id,String name) {
		String sql = "update t_fileExpress_file set fileName='"+name+"' where id='"+id+"'";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
	}
	
	public void deleteGuoQi(Integer id,String path) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressFile.class);
		SimpleExpression a = Restrictions.eq("state", 1);
		criteria.add(a);
		DetachedCriteria criteria1 = criteria.createCriteria("fileExpressGroup");
		DetachedCriteria criteria2 = criteria1.createCriteria("tjsUser");
		SimpleExpression b = Restrictions.eq("userId", id);
		criteria2.add(b);
		
		List<FileExpressFile> list = qbcDao.getCriteria(criteria);
		String sql = "delete from t_fileExpress_file where id in(";
		if (list.size()>0) {
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				FileExpressFile file = (FileExpressFile) iterator.next();
				String name = file.getFileName();
				String strdos = "cmd /c del \""+path+name+"\"";
	 		   	Process process;
				try {
					process = Runtime.getRuntime().exec(strdos);
					process.waitFor();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				sql += "'"+file.getId()+"',";
			}
			sql = sql.substring(0, sql.length()-1)+")";
			jdbcdao.executeUpdate(sql);
			jdbcdao.closeStmt();
			jdbcdao.closeConn();
		}
	}
	
	public int deleteFile(String ids,int page,String dirName) {
		String[] inte = new String[ids.split(",").length];
		for (int j = 0; j < inte.length; j++) {
			if (ids.split(",")[j] != null) {
				inte[j] = ids.split(",")[j];
			}
		}
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressFile.class);
		Criterion a = Restrictions.in("id", inte);
		criteria.add(a);
		List list = qbcDao.getCriteria(criteria);
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			FileExpressFile file = (FileExpressFile) iterator.next();
			
			String sql = "update t_fileExpress_file set isdel=1 where id='"+file.getId()+"'";
			jdbcdao.executeUpdate(sql);
		}
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
		return page;
	}
	
	public FileExpressFile findById(String id) {
		FileExpressFile file = expressFileDAO.findById(id);
		return file;
	}
	
	public void MoveFile(String copyid,String copygroup) {
		String sql = "update t_fileExpress_file set group_id='"+copygroup+"' where id in("+copyid+")";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
	}
	
	public List getExpressFile(int userid) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressFile.class);
		//SimpleExpression a = Restrictions.eq("state", 0);
		//criteria.add(a);
		DetachedCriteria criteria1 = criteria.createCriteria("tjsUser");
		SimpleExpression b = Restrictions.eq("userId", userid);
		criteria1.add(b);
		criteria.addOrder(Order.desc("datetime"));
		List list = qbcDao.getCriteria(criteria);
		return list;
	}
	
	public List selectFileByName(String fileName,Integer user_id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressFile.class);
		criteria.add(Restrictions.isNull("isdel"));
		//SimpleExpression a = Restrictions.eq("state", 0);
		SimpleExpression b = Restrictions.like("fileName", "%"+fileName+"%");
		//LogicalExpression c = Restrictions.and(a, b);
		criteria.add(b);
		
		DetachedCriteria criteria1 = criteria.createCriteria("fileExpressGroup");
		DetachedCriteria criteria2 = criteria1.createCriteria("tjsUser");
		SimpleExpression d = Restrictions.eq("userId", user_id);
		criteria2.add(d);
		
		criteria.addOrder(Order.desc("datetime"));
		List list = qbcDao.getCriteria(criteria);
		return list;
	}
	
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	public List selectHighFile(String lie,String value,Integer userid) {
		Date date = null;
		Date date1 = null;
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressFile.class);
		criteria.add(Restrictions.isNull("isdel"));
		//SimpleExpression a = Restrictions.eq("state", 0);
		//criteria.add(a);
		DetachedCriteria criteria1 = criteria.createCriteria("fileExpressGroup");
		DetachedCriteria criteria2 = criteria1.createCriteria("tjsUser");
		SimpleExpression b = Restrictions.eq("userId", userid);
		criteria2.add(b);
		if (lie.equals("fileName")) {
			SimpleExpression c = Restrictions.like("fileName", "%"+value+"%");
			criteria.add(c);
		}else if (lie.equals("username")) {
			if (value!=null && !"".equals(value)) {
				DetachedCriteria criteria3 = criteria.createCriteria("tjsUser");
				SimpleExpression d = Restrictions.like("userName", "%"+value+"%");
				criteria3.add(d);
			}
		}else if (lie.equals("datetime")) {
			if (value!=null && !"".equals(value)) {
				try {
					date = sdf.parse(value);
					date1 = DayFormat.afterNDay(date, -1);
					
					value = value+" 23:59:59";
					SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					date = sdf1.parse(value);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				Criterion e = Restrictions.between("datetime", date1, date);
				criteria.add(e);
			}
		}else if (lie.equals("level")) {
			SimpleExpression f = Restrictions.eq("expressLevel", Integer.valueOf(value));
			criteria.add(f);
		}
		List list = qbcDao.getCriteria(criteria);
		return list;
	}
	
	public void insert(String id,String group_id,int expressUser_id,String fileName,String datetime,String expressLevel,String fileSize,String filePath,String remark) {
		String sql = "insert into t_fileExpress_file(id,group_id,expressUser_id,fileName,datetime,expressLevel,fileSize,filePath,remark,state) values('"+id+"','"+group_id+"',"+expressUser_id+",'"+fileName+"','"+datetime+"',"+expressLevel+","+fileSize+",'"+filePath+"','"+remark+"',1)";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
	}
	
	public void updateState(String filename,String state) {
		String sql = "update t_fileExpress_file set state="+state+" where fileName in("+filename+")";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
	}
	
	public void updateStateByUserId(Integer userid,String state) {
		String sql = "update t_fileExpress_file set state="+state+" where group_id in(select id from t_fileExpress_group where tjsuser_id='"+userid+"')";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
	}
	
	public String downloadFile(String id,String filename,String dirName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressFile.class);
		SimpleExpression a = Restrictions.eq("id", id);
		criteria.add(a);
		FileExpressFile expressFile = (FileExpressFile) qbcDao.getCriteria(criteria).iterator().next();
		
		dirName += expressFile.getFileExpressGroup().getTjsUser().getUuid()+"\\";
		File file=new File(dirName+"download\\"); 
		String test[]; 
		test=file.list();
		if (test.length>0) {
			for (int i = 0; i < test.length; i++) {
				String strdos = "cmd /c del \""+dirName+"download\\"+test[i]+"\"";
				Process process;
				try {
					process = Runtime
							.getRuntime().exec(strdos);
					process.waitFor();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		String strdos = "cmd /c copy \""+dirName+filename+ "\" \""+dirName+"download\\\"";
		Process process;
		try {
			process = Runtime
					.getRuntime().exec(strdos);
			process.waitFor();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File file1 = new File(dirName+"download\\"+filename.split("@&_&@")[1]);
		File oldFile = new File(dirName+"download\\"+filename);
		oldFile.renameTo(file1);
		return expressFile.getFileExpressGroup().getTjsUser().getUuid();
	}
	
	public void updateFileName(String id,String fileName,String dirName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileExpressFile.class);
		SimpleExpression a = Restrictions.eq("id", id);
		criteria.add(a);
		FileExpressFile expressFile = (FileExpressFile) qbcDao.getCriteria(criteria).iterator().next();
		while (fileName.indexOf("  ")!=-1) {
			fileName = fileName.replace("  ", " ");
		}
		String sql = "update t_fileExpress_file set fileName='"+fileName+"' where id='"+id+"'";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
		
		dirName += expressFile.getFileExpressGroup().getTjsUser().getUuid()+"\\";
		String strdos = "cmd /c rename \""+dirName+expressFile.getFileName()+"\" \""+fileName+"\"";
		Process process;
		try {
			process = Runtime
					.getRuntime().exec(strdos);
			process.waitFor();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
